<?php

namespace app\common\model;
use \think\Model;

class JdWx 
{
    private $AppKey = '279f2acf486db68921be24f888d11930'; 
    private $url = 'https://way.jd.com/yingyan/idcard';

    /**
    * 验证真实姓名
    * @param real_name string 真实姓名
    * @param IDcard string 身份证号
    */
    public function _verifyName($realName, $IDcard){
        header("Content-type: application/json; charset=utf-8");
        $params = array(
            'cardno' => $IDcard,
            'name' => $realName,
            'appkey' => $this->AppKey,
        );
        $res = $this->wx_http_request($this->url, $params);
        $res = json_decode($res, true);
        if($res['code'] = 10000){
            if(empty($res['result'])){
                return false;
            }
            $result = $res['result'];
            if($result['resp']['code'] == 0){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    
    public function wx_http_request($url, $params, $body="", $isPost=false, $isImage=false ) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url."?".http_build_query($params));
        if($isPost){
            if($isImage){
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                        'Content-Type: multipart/form-data;',
                        "Content-Length: ".strlen($body)
                    )
                );
            }else{
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                        'Content-Type: text/plain'
                    )
                );
            }
            curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
        }
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}
